using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using WP.DB;
using WP.Modules;
using WP.Commons;

namespace WP.MF.Form
{
    public partial class frmMWindowDictMtn:BaseMtn
    {
        private DBAccess dbAccess = null;
        private bool mblnListEndFill = false;
        public frmMWindowDictMtn()
        {
            InitializeComponent();
            dbAccess = new DBAccess(false, false);
            LoadAllControl(new ArrayList(this.Controls));
            this.MinimumSize = this.Size;
        }

        private void frmMLanguageDictMtn_Load(object sender, EventArgs e)
        {
            ShowLanguageMenu();
            cboModule.SelectedIndex = 0;
        }
        private void frmMWindowDictMtn_Resize(object sender, EventArgs e)
        {
            dgList.Focus();
        }
        private void frmMLanguageDictMtn_FormClosed(object sender, FormClosedEventArgs e)
        {
            dbAccess.CloseCon();
        }
        private void cboModule_SelectedIndexChanged(object sender, EventArgs e)
        {
            string strSql = string.Empty;
            switch (cboModule.Text.ToUpper())
            {
                case "AP":
                case "AR":
                case "ES":
                case "GL":
                case "IM":
                case "OE":
                case "PO":
                case "RM":
                case "TP":
                    strSql = "select distinct ProgName from mLanguageDict where ProgName like 'frm" + cboModule.Text + "%'";
                    break;
                case "MF":
                    strSql = "select distinct ProgName from mLanguageDict where ProgName like 'frmM%' and ProgName in (select Net_ProgName from mMenu)";
                    break;
                default:
                    strSql = "select distinct ProgName from mLanguageDict where Left(ProgName,5)<>'frmGL'" +
                             " and Left(ProgName,5)<>'frmAP' and Left(ProgName,5)<>'frmAR'" +
                             " and Left(ProgName,5)<>'frmOE' and Left(ProgName,5)<>'frmIM'" +
                             " and Left(ProgName,5)<>'frmPO' and Left(ProgName,5)<>'frmRM'" +
                             " and Left(ProgName,5)<>'frmTP' and Left(ProgName,5)<>'frmES'" +
                             " and ProgName Not in (select Net_ProgName from mMenu) and ProgName<>''";
                    break;
            }
            DataTable DtProg = dbAccess.GetDataTable(strSql);
            cboWindow.Items.Clear();
            cboWindow.Text = "";
            foreach (DataRow DrProg in DtProg.Rows)
            {
                cboWindow.Items.Add(DrProg["ProgName"].ToString().Trim());
            }
            if (cboWindow.Items.Count > 0)
            {
                cboWindow.SelectedIndex = 0;
            }
            else
            {
                dgList.DataSource = null;
            }
        }
        private void cboWindow_SelectedIndexChanged(object sender, EventArgs e)
        {
            FillList();
        }
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnHelp_Click(object sender, EventArgs e)
        {
            base.OnHelp();
        }
        private void dgList_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            string strValue = string.Empty;
            if (dgList.Rows.Count > 0 && mblnListEndFill)
            {
                strValue = dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();
                if (strValue.Length > 100)
                {
                    dgList.Rows[e.RowIndex].Cells[e.ColumnIndex].Value = strValue.Substring(0, 100);
                }
                UpdateChange(e.RowIndex, e.ColumnIndex);
            }
        }

        public void ShowForm()
        {
            this.MdiParent = SysVar.MDIParentForm;
            this.Show();
        }
        private void FillList()
        {
            string strMenuId=DataFunctions.GetFieldValue("mMenu","MenuId","Net_ProgName='" + cboWindow.Text +"' order by Net_Parameter");
            MenuInfo menuInfo=DataFunctions.GetMenuInfo(strMenuId);   
            string strTable="";
            Size sizeNormal;
            System.Windows.Forms.Form  frmobj = ReflectMethod.GetForm(menuInfo, false, out sizeNormal, out strTable);
            BaseMtn oBaseMtn = frmobj as BaseMtn;
            BaseQryRpt oBaseQryRpt = frmobj as BaseQryRpt;
            if (oBaseMtn != null)
            {
                oBaseMtn.InitLanguageSetting();
            }
            else if (oBaseQryRpt != null)
            {
                oBaseQryRpt.InitLanguageSetting();
            }
            if(frmobj!=null) frmobj.Close(); 
            string strSql = string.Empty;
            strSql = "select * from mLanguage order by LanguageSeqNo";
            DataTable DtLanguage = dbAccess.GetDataTable(strSql);
            strSql = "select recid,rtrim(ltrim(ID)) as Name,";
            foreach (DataRow DrLanguage in DtLanguage.Rows)
            {
                strSql += "rtrim(ltrim(Language" + DrLanguage["LanguageSeqNo"].ToString() + "Text)) as " + DrLanguage["Language"].ToString().Trim() + ",";
            }
            strSql += " ltrim(rtrim(Description)) as Description from mLanguageDict where ProgName='" + cboWindow.Text + "' and ID not like 'MSG_%'";
            mblnListEndFill = false;
            DataTable DtLanguageDict = dbAccess.GetDataTable(strSql);
            dgList.DataSource = DtLanguageDict;
            AdjustGrid();
            mblnListEndFill = true;
        }
        private void AdjustGrid()
        {
            this.dgList.RowHeadersWidth =5;
            this.dgList.RowHeadersVisible = false;
            this.dgList.ColumnHeadersHeight = 20;
            this.dgList.Columns["RecID"].Visible = false;
            this.dgList.Columns["RecID"].Width = 0;
            this.dgList.Columns["RecID"].Frozen = true;
            this.dgList.Columns[2].ReadOnly = true;
            this.dgList.Columns["Name"].ReadOnly = true;
            this.dgList.Columns["Name"].Frozen = true;
            this.dgList.Columns["Name"].DefaultCellStyle.BackColor=System.Drawing.SystemColors.Control;
            int intWidth = 0;
            for (int i = 1; i <= dgList.Columns.Count - 2; i++)
            {
                dgList.Columns[i].Width = 100;
                intWidth += dgList.Columns[i].Width;
            }
            this.dgList.Columns[dgList.Columns.Count - 1].Width = this.Width - intWidth;
            for (int i = 0; i <= dgList.Rows.Count - 1; i++)
            {
                dgList.Rows[i].Height = 18;
            }
        }
        private bool UpdateChange(int irowNo, int iColNo)
        {
            string strSql = string.Empty;
            int intText=0;
            if (dgList.Columns[iColNo].HeaderText == "Description")
            {
                strSql = "update mLanguageDict set Description='" + dgList.Rows[irowNo].Cells[iColNo].Value.ToString() + "' where RecID=" + dgList.Rows[irowNo].Cells[0].Value.ToString();
            }
            else
            {
                strSql = "select language,languageSeqNo from mLanguage where Language<>'English' order by LanguageSeqNo";
                DataTable DtLanguage = dbAccess.GetDataTable(strSql);
                foreach (DataRow DrLanguage in DtLanguage.Rows)
                {
                    if (DrLanguage["language"].ToString().Trim() == dgList.Columns[iColNo].HeaderText)
                    {
                        intText = CvtFunc.ToInt(DrLanguage["languageSeqNo"]);
                    }
                }
                strSql = "update mLanguageDict set Language" + intText.ToString() + "Text='" + dgList.Rows[irowNo].Cells[iColNo].Value.ToString() + "' where RecID=" + dgList.Rows[irowNo].Cells[0].Value.ToString();
            }
            dbAccess.ExecuteSql(strSql);
            return true;
        }
        private void LanguageMenuItem_Click(object sender, EventArgs e)
        {
            btnLanguage.ShowDropDown();
            ToolStripMenuItem ToolMenu =(ToolStripMenuItem) sender;
            ToolMenu.Checked = (ToolMenu.Checked ? false:true);
            dgList.Columns[ToolMenu.Text.Trim()].Visible = ToolMenu.Checked;
        }
        private void ShowLanguageMenu()
        {
            DataView dv = dbAccess.GetDataTable("select * from mLanguage").DefaultView;
            dv.RowFilter = "LanguageSeqNo=2";
            if (dv.Count != 0)
            {
                language2ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language2ToolStripMenuItem.Checked = true;
                language2ToolStripMenuItem.Visible = true;
            }
            else
            {
                language2ToolStripMenuItem.Checked = false;
                language2ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=3";
            if (dv.Count != 0)
            {
                language3ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language3ToolStripMenuItem.Checked = true;
                language3ToolStripMenuItem.Visible = true;
            }
            else
            {
                language3ToolStripMenuItem.Visible = false;
                language3ToolStripMenuItem.Checked = false;
            }
            dv.RowFilter = "LanguageSeqNo=4";
            if (dv.Count != 0)
            {
                language4ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language4ToolStripMenuItem.Checked = true;
                language4ToolStripMenuItem.Visible = true;
            }
            else
            {
                language4ToolStripMenuItem.Checked = false;
                language4ToolStripMenuItem.Visible = false;
            } 
            dv.RowFilter = "LanguageSeqNo=5";
            if (dv.Count != 0)
            {
                language5ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language5ToolStripMenuItem.Checked = true;
                language5ToolStripMenuItem.Visible = true;
            }
            else
            {
                language5ToolStripMenuItem.Checked = false;
                language5ToolStripMenuItem.Visible = false;
            }
            dv.RowFilter = "LanguageSeqNo=6";
            if (dv.Count != 0)
            {
                language6ToolStripMenuItem.Text = dv[0]["Language"].ToString();
                language6ToolStripMenuItem.Checked = true;
                language6ToolStripMenuItem.Visible = true;
            }
            else
            {
                language6ToolStripMenuItem.Checked = false;
                language6ToolStripMenuItem.Visible = false;
            }
        }

    }
}